import Cookies from 'js-cookie';
import {encrypt, decrypt} from './utils';
import {Base64} from 'js-base64';
import {LOGIN_KEY, STATION_KEY, EXPIRES, SAVEPASSWROLD_KEY, SAVEUSERTELL_KEY} from '@/constants';

export const User = {
    getUserInfo() {
        const info = Cookies.get(LOGIN_KEY);

        if (info) {
            return JSON.parse(decrypt(info));
        }
        return false;
    },
    setUserInfo(token) {
        return Cookies.set(LOGIN_KEY, encrypt(JSON.stringify(token)), {expires: EXPIRES});
    },
    removeUserInfo() {
        Cookies.remove(STATION_KEY);
        return Cookies.remove(LOGIN_KEY);
    }
};

export const Station = {

    getStations() {
        const info = Cookies.get(STATION_KEY);

        if (info) {
            return JSON.parse(decrypt(info));
        }
        return false;
    },

    setStations(info) {
        return Cookies.set(STATION_KEY, encrypt(JSON.stringify(info)), {expires: EXPIRES});
    },
    removeStations() {
        return Cookies.remove(STATION_KEY);
    }
};

export const SavePassworld = {

    getSavePassworld() {
        const info = Cookies.get(SAVEPASSWROLD_KEY);

        if (info) {
            return JSON.parse(decrypt(info));
        }
        return false;
    },
    setSavePassworld(info) {
        return Cookies.set(SAVEPASSWROLD_KEY, encrypt(JSON.stringify(info)), {expires: EXPIRES});
    },

    removeSavePassworld() {
        return Cookies.remove(SAVEPASSWROLD_KEY);
    }
};

export const SaveUserTell = {

    getSaveUserTell() {
        const info = Cookies.get(SAVEUSERTELL_KEY);

        if (info) {
            return JSON.parse(decrypt(info));
        }
        return false;
    },
    setSaveUserTell(info) {
        return Cookies.set(SAVEUSERTELL_KEY, encrypt(JSON.stringify(info)), {expires: EXPIRES});
    },
    removeSaveUserTell() {
        return Cookies.remove(SAVEUSERTELL_KEY);
    }
};

export const DecryptToken = {

    getUserInfo(token) {

        let userInfo = Base64.decode(token.split('.')[1]);

        return JSON.parse(userInfo);
    }
};